package com.cy.controller;

import java.awt.Point;
import java.util.ArrayList;
import java.util.Random;

import com.cy.member.Che;
import com.cy.member.Chessboard;
import com.cy.member.ControlPanel;
import com.cy.member.Jiang;
import com.cy.member.Ma;
import com.cy.member.Pao;
import com.cy.member.Shi;
import com.cy.member.Step;
import com.cy.member.Xiang;
import com.cy.member.Zu;
import com.cy.member.parent.ChineseChessman;
/**
 * 总控制器
 * @author CY
 *
 */
public class MasterController {
	//棋盘的引用
	private static Chessboard chessboard;
	//控制面板的引用
	private static ControlPanel controlPanel;
	//标记该哪方出棋
	private static boolean who = true;
	//判断是否被点击过
	private static boolean click = false;
	//要被移动的棋子
	private static ChineseChessman storeChineseChessman = null;
	//记录走的每一步棋
	private static ArrayList<Step> steps = new ArrayList<Step>();
	//当前的游戏模式 true经典模式，false流行模式
	public static boolean gameType = true;
	private static int redCount = 16;
	private static int blackCount = 16;
	private static ArrayList<ChineseChessman> redChineseChessmans = new ArrayList<ChineseChessman>();
	private static ArrayList<ChineseChessman> blackChineseChessmans = new ArrayList<ChineseChessman>();
	
	/**
	 * 返回一个随机的棋子
	 * @return
	 */
	private static ChineseChessman returnChineseChessman() {
		int temp;
		ChineseChessman chineseChessman;
		//返回一个红色随机棋子
		if((who && redCount > 0) || (!who && blackCount == 0)) {
			temp = new Random().nextInt(redCount);
			redCount--;
			chineseChessman = redChineseChessmans.get(temp);
			redChineseChessmans.remove(temp);
		} else {//返回一个黑色随机棋子
			temp = new Random().nextInt(blackCount);
			blackCount--;
			chineseChessman = blackChineseChessmans.get(temp);
			blackChineseChessmans.remove(temp);
		}
		return chineseChessman;
	}

	/**
	 * 初始化ChineseChessmans
	 */
	private static void initChineseChessmans() {
		redCount = 16;
		blackCount = 16;
		//添加黑色棋子
		boolean who = false;
		ArrayList<ChineseChessman> chineseChessmans;
		for(int i = 0; i < 2; i++) {
			if(who) {
				chineseChessmans = redChineseChessmans;
			} else {
				chineseChessmans = blackChineseChessmans;
			}
			//添加将
			ChineseChessman jiang = new Jiang(who);
			chineseChessmans.add(jiang);
			//添加车
			for(int j = 0; j < 2; j++) {
				ChineseChessman che = new Che(who);
				chineseChessmans.add(che);
			}
			//添加马
			for(int j = 0; j < 2; j++) {
				ChineseChessman ma = new Ma(who);
				chineseChessmans.add(ma);
			}
			//添加象
			for(int j = 0; j < 2; j++) {
				ChineseChessman xiang = new Xiang(who);
				chineseChessmans.add(xiang);
			}
			//添加士
			for(int j = 0; j < 2; j++) {
				ChineseChessman shi = new Shi(who);
				chineseChessmans.add(shi);
			}
			//添加炮
			for(int j = 0; j < 2; j++) {
				ChineseChessman pao = new Pao(who);
				chineseChessmans.add(pao);
			}
			//添加卒
			for(int j = 0; j < 5; j++) {
				//通过异或来改变卒子在上还是在下
				ChineseChessman zu = new Zu(who);
				chineseChessmans.add(zu);
			}
			//添加红色棋子
			who = !who;
		}
	}
	
	/**
	 * 开启控制
	 * @param _chessboard 被控制的棋盘
	 * @param _controlPanel 被控制的控制面板
	 */
	public static void Controller(Chessboard _chessboard, ControlPanel _controlPanel){
		chessboard = _chessboard;
		controlPanel = _controlPanel;
	}
	
	/**
	 * 坐标被点击
	 * @param site
	 */
	public static void Clicked(Point site) {
		Point p = null;
		//存储要被移动棋子的原始位置
		if(storeChineseChessman != null){
			p = storeChineseChessman.getChessman().getSite();
		}
		//要移动棋子，第二次点击
		if(click && site != null && chessboard.moveChineseChessman(storeChineseChessman, site)) {
			//播放移动棋子的声音
			MusicController.playGo();
			//另一方走棋
			who = !who;
			//说明下一次点击为选择性点击
			click = false;
			//修改显示该谁出棋
			controlPanel.setWho(who);
			//记录每一步
			steps.add(new Step(storeChineseChessman, p, site));
		} else {
			MusicController.playErr();
		}
	}
	
	/**
	 * 棋子被点击
	 * @param chessman
	 */
	public static void Clicked(ChineseChessman chineseChessman) {
		//流行模式,棋子未被翻转
		if(!gameType && ChineseChessman.class == chineseChessman.getClass()){
			ChineseChessman _chineseChessman = returnChineseChessman();
			_chineseChessman.setChessman(chineseChessman.getChessman());
			chessboard.replaceChessman(_chineseChessman, chineseChessman);
			who = !who;
			controlPanel.setWho(who);
			//说明下一次点击为选择性点击
			click = false;
			//播放选择棋子的声音
			MusicController.playSelect();
		} else if(!chineseChessman.getChessman().isWho() ^ who) {//保证点击的棋子是要走的一方,第一次点击
			//保存被选择的棋子
			storeChineseChessman = chineseChessman;
			//修改hint,选择性hint
			chessboard.selectChessman(chineseChessman);
			//说明下一次为移动性点击
			click = true;
			//播放选择棋子的声音
			MusicController.playSelect();
		} else if (chineseChessman.getChessman().isWho() ^ who && click) {//尝试吃掉对方的棋子，第二次点击
			//存储要被移动棋子的原始位置
			Point p = storeChineseChessman.getChessman().getSite();
			//将棋子移动到被吃棋子的位置
			if(chessboard.eatChineseChessman(storeChineseChessman,chineseChessman)){
				//播放吃棋子的声音
				MusicController.playEat();
				//说明下一次点击为选择性点击
				click = false;
				//另一方走棋
				who = !who;
				//修改显示该谁出棋
				controlPanel.setWho(who);
				//判断是否结束游戏
				if(chineseChessman.getClass().toString().equals(Jiang.class.toString())) {
					chessboard.setWin(storeChineseChessman.getChessman().isWho());
					MusicController.playGamewin();
				}
				//记录每一步
				Step step = new Step(storeChineseChessman, p, chineseChessman.getChessman().getSite());
				step.setKilled(chineseChessman);
				steps.add(step);
			} else {
				MusicController.playErr();
			}
		} else {
			MusicController.playErr();
		}
	}
	
	/**
	 * 新游戏
	 */
	public static void newGame() {
		//新游戏
		chessboard.newGame(gameType);
		who = true;
		controlPanel.setWho(who);
		chessboard.update(chessboard.getGraphics());
		steps.clear();
		if(!gameType) {
			initChineseChessmans();
		}
	}

	/**
	 * 获取棋盘的引用
	 * @return
	 */
	public static Chessboard getChessboard() {
		return chessboard;
	}
	
	/**
	 * 悔棋
	 */
	public static void back() {
		if(!steps.isEmpty()) {
			//获取上一步
			Step step = steps.get(steps.size()-1);
			steps.remove(steps.size()-1);
			//归位
			chessboard.back(step);
			who = !who;
			controlPanel.setWho(who);
		}
	}
	
	/**
	 * 开关控制面板
	 * @return
	 */
	public static boolean switchControlPanel() {
		controlPanel.setVisible(!controlPanel.isVisible());
		return controlPanel.isVisible();
	}

	/**
	 * 获取控制面板的引用
	 * @return
	 */
	public static ControlPanel getControlPanel() {
		return controlPanel;
	}
	
}
